package com.jshongli.myapplication.task.fragment;

import android.annotation.SuppressLint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jshongli.myapplication.R;
import com.jshongli.myapplication.base.BaseFragment;
import com.jshongli.myapplication.common.callback.ResponseCallback;
import com.jshongli.myapplication.common.utils.AccountManager;
import com.jshongli.myapplication.common.utils.DateUtils;
import com.jshongli.myapplication.common.utils.ParseUtils;
import com.jshongli.myapplication.common.utils.ToastUtils;
import com.jshongli.myapplication.common.utils.URLUtils;
import com.jshongli.myapplication.model.task.ReplyBean;
import com.jshongli.myapplication.model.task.TaskBean;

import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import okhttp3.Request;

@SuppressLint("ValidFragment")
public class TaskDetailFragment extends BaseFragment {

    TaskBean.DATA item;

    public TaskDetailFragment(TaskBean.DATA item) {
        this.item = item;
    }

    //region 同
    @BindView(R.id.tv_bt)
    TextView tv_bt;
    @BindView(R.id.tv_fqr)
    TextView tv_fqr;

    @BindView(R.id.tv_jzsj)
    TextView tv_jzsj;
    @BindView(R.id.tv_nr)
    TextView tv_nr;
    @BindView(R.id.tv_clr)
    TextView tv_clr;
    @BindView(R.id.tv_zt)
    TextView tv_zt;

    @BindView(R.id.recy)
    RecyclerView recy;
    //endregion




    @Override
    protected int getLayoutId() {
        return R.layout.fragment_task_detail;
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initView() {
        super.initView();

        setCommon();

        getData();
    }

    private void setCommon() {
        tv_bt.setText(item.title);
        tv_fqr.setText(item.createName);
        tv_jzsj.setText(item.finishTm);
        tv_nr.setText(item.content);
        tv_clr.setText(item.userName);
        tv_zt.setText(item.statusName);
    }





    //region HTTP -- 处理过程
    public void getData() {
        String url = URLUtils.URLNoToken("manageAPI/bizReply/getList");
        Map<String, String> map = new HashMap<>();
        map.put("workId", String.valueOf(item.id));
        OkHttpUtils
                .get()
                .url(url)
                .params(map)
                .addHeader("Authorization", AccountManager.getToken())
                .tag(this)
                .build()
                .execute(new ResponseCallback<ReplyBean>(mContext) {

                    @Override
                    public void onBefore(Request request, int id) {
                        super.onBefore(request, id);
                    }

                    @Override
                    public void onAfter(int id) {
                        super.onAfter(id);

                        if(mProcessData.size()>0){
                            recy.setVisibility(View.VISIBLE);
                        }else {
                            recy.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onResponse(ReplyBean response, int id) {
                        super.onResponse(response, id);
                        if (response.code == 0) {
                            if (response.data != null) {
                                mProcessData = response.data;
                            }
                        }  else {
                            ToastUtils.show(mContext, "查询出错");
                        }




                        if (mAdapter == null) {
                            initRecy();
                        } else {
                            mAdapter.setNewData(mProcessData);
                        }
                    }

                });
    }
    //endregion

    //region Recy -- 处理过程
    BaseQuickAdapter<ReplyBean.DATA, BaseViewHolder> mAdapter;
    List<ReplyBean.DATA> mProcessData = new ArrayList<>();

    private void initRecy() {
        mAdapter = new BaseQuickAdapter<ReplyBean.DATA, BaseViewHolder>(R.layout.list_task_process_item, mProcessData) {
            @Override
            protected void convert(BaseViewHolder helper, ReplyBean.DATA item) {
                helper.setText(R.id.tv_tm, DateUtils.formatString(item.createTime,"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd\nHH:mm:ss"))
                        .setText(R.id.tv_ry,item.createName);
                helper.setText(R.id.tv_zt,item.statusName);
                helper.setText(R.id.tv_content, item.reply);

                }
        };

        recy.setAdapter(mAdapter);
        recy.setLayoutManager(new LinearLayoutManager(mContext));
    }
    //endregion

}
